import { observable, action } from 'mobx-miniprogram'
import wxUtils from "@src/common/wxUtils";

export default observable({
  // 数据字段
  auth: false,
  ask: false,
  latitude: null,
  longitude: null,
  // actions
  getLocation: action(function (showTips = false) {
    return new Promise((resolve, reject) => {
      this.getAuth().then(res => {
        wxUtils.getLocation({ tips: true }).then((res) => {
          this.latitude = res.latitude;
          this.longitude = res.longitude;
          resolve && resolve({ latitude: res.latitude, longitude: res.longitude });
        }).catch((error) => {
          if(this.longitude&&this.latitude){
            this.auth = true;
            resolve && resolve({ latitude: this.latitude, longitude: this.longitude });
            return
          }
          this.auth = false;
          reject && reject(error);
        })
      }).catch(error => {
        reject && reject(error);
        if (showTips&&!this.longitude&&!this.latitude) {
          // this.showAuthTip(error);
        }
      })
    })
  }),
  getAuth: action(function () {
    return new Promise((resolve, reject) => {
      if (this.auth) {
        resolve();
        return;
      }
      // else if (this.ask) {
      //   reject();
      //   return;
      // }
      if(this.longitude&&this.latitude){
        resolve()
        return
      }
      wxUtils.authorize("scope.userLocation").then(res => {
        console.log('reswxUtils.authorize', res)
        this.ask = true;
        this.auth = true;
        resolve();
      }).catch((error) => {
        this.ask = true;
        this.auth = false;
        reject(error)
      })
    })
  }),
  checkAuth: action(function () {
    wxUtils.getSetting().then(res => {
      this.ask = res.authSetting.hasOwnProperty("scope.userLocation");
      this.auth = !!res.authSetting['scope.userLocation']
    })
  }),
  showAuthTip: action(function (err) {
    console.log('err', err);
    const self = this;
    wx.showModal({
      title: '位置授权提示',
      content: '由于您拒绝了授权,无法获取距离信息,如需获取,请开启位置权限后,请刷新重新获取定位',
      showCancel: false,
      success(res) {

      }
    })
    // if (err.errMsg.indexOf("auth deny") !== -1 ||
    //   err.errMsg.indexOf("authorize no response") !== -1 ||
    //   err.errMsg.indexOf("auth denied") !== -1) {
    //   wx.showModal({
    //     title: '位置授权提示',
    //     content: '由于您拒绝了授权,无法获取距离信息,如需获取,请开启位置权限后,请刷新重新获取定位',
    //     showCancel: false,
    //     success(res) {
    //     }
    //   })
    // }

    // else if (err.errMsg.indexOf("getLocation:fail") !== -1) {
    //   wx.showModal({
    //     title: '位置授权提示',
    //     content: '您手机未开启定位功能，开启后请刷新重新获取定位',
    //     showCancel: false,
    //     success(res) {
    //     }
    //   })
    // } else {
    //   wx.showModal({
    //     title: '位置授权提示',
    //     content: '获取定位异常，开启授权后请刷新重新获取定位',
    //     showCancel: false,
    //     success(res) {
    //     }
    //   })
    // }
  }),
  //强行修改auth值，慎用！！
  setAuth: action(function () {
    this.auth = true;
  }),
})


